<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="兑换商品名称" prop="giftName">
      <el-input v-model="dataForm.giftName" placeholder="兑换商品名称"></el-input>
      <el-button type="primary" @click="addGoodsBySingle">单品添加商品</el-button>
    </el-form-item>
    <!-- <el-form-item label="兑换商品价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="兑换商品价格"></el-input>
    </el-form-item> -->
    <el-form-item label="兑换商品所需积分" prop="integral">
      <el-input v-model="dataForm.integral" placeholder="兑换商品所需积分"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>

    <!-- 弹窗, 商品查询 -->
    <goods-list v-if="goodsListVisible" ref="sinleGoodsList" @updateForm="updateFormData"></goods-list>
  </el-dialog>
</template>

<script>
    import {getWxGiftObj, addWxGiftObj, putWxGiftObj} from '@/api/mp/wxgift'
    import GoodsList from './goods-search-single'
    export default {
    data () {
      return {
        visible: false,
        goodsListVisible: false,
        dataForm: {
          id: 0,
          giftName: '',
          price: '',
          integral: '',
          createTime: '',
          createUser: '',
          updateTime: '',
          updateUser: '',
          delFlag: '',
          tenantId: '',
          goodsId: '',
        },
        dataRule: {
          giftName: [
            { required: true, message: '兑换商品名称不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '兑换商品价格不能为空', trigger: 'blur' }
          ],
          integral: [
            { required: true, message: '兑换商品所需积分不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          updateUser: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '删除标识(1-未删除,-1已删除)不能为空', trigger: 'blur' }
          ],
          tenantId: [
            { required: true, message: '租户Id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      GoodsList
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getWxGiftObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putWxGiftObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                addWxGiftObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      },
      // 打开商品列表弹窗
      addGoodsBySingle() {
        this.goodsListVisible = true;
        this.$nextTick(() => {
          this.$refs.sinleGoodsList.init({});
        });
      },
      //更新表单数据
      updateFormData(data) {
        this.dataForm.goodsId = data.data.id
        this.dataForm.giftName = data.data.viewGoodsName
      },
    }
  }
</script>
